﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMDTO;
using Ext.Net;
using IMBL;
using System.Collections.ObjectModel;
using System.Collections;

namespace IMPPFront
{
    public partial class Persons : System.Web.UI.Page
    {
        private String personsGridFilter;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void GotoPersoonlijkPlan(object sender, DirectEventArgs e)
        {
            Response.Redirect("PP.aspx?PersonID=" + e.ExtraParams["id"], true);
        }

        public void FilterGrid(object sender, DirectEventArgs e)
        {
            X.Msg.Notify("TextField", "Text : " + tfFilter.Text).Show();
            PersonsGrid.Reload();
        }

        public void Persons_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTONaturalPersonShort> data = new IMBLNaturalPerson().GetNaturalPersonShortsByName(tfFilter.Text);
   
            if (e != null)
            {
                personsGridFilter = e.Parameters[this.PersonsGridFilters.ParamPrefix];
            }

           //sorting
            if (!string.IsNullOrEmpty(e.Sort))
            {
                data.Sort(delegate(IMDTONaturalPersonShort x, IMDTONaturalPersonShort y)
                {
                    int direction = e.Dir == Ext.Net.SortDirection.DESC ? -1 : 1;

                    if (e.Sort.Equals("idNaturalPerson"))
                    {
                        return CaseInsensitiveComparer.Default.Compare(x.idNaturalPerson, y.idNaturalPerson) * direction;
                    }
                    else if (e.Sort.Equals("FirstName"))
                    {
                        return CaseInsensitiveComparer.Default.Compare(x.FirstName, y.FirstName) * direction;
                    }
                    else
                    {
                        return CaseInsensitiveComparer.Default.Compare(x.LastName, y.LastName) * direction;
                    }
                });
            }

            //paging
            var limit = e.Limit;

            if ((e.Start + e.Limit) > data.Count)
            {
                limit = data.Count - e.Start;
            }

            List<IMDTONaturalPersonShort> rangeData = (e.Start < 0 || limit < 0) ? data : data.GetRange(e.Start, limit);

            e.Total = data.Count;

            this.PersonsGrid.GetStore().DataSource = rangeData;
        }
    }
}